# Template file for 'opencv4'
pkgname=opencv4
version=4.3.0
revision=4
wrksrc=opencv-${version}
build_style=cmake
configure_args="-DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_OPENMP=ON -DWITH_OPENCL=ON
 -DOPENCV_GENERATE_PKGCONFIG=YES
 -DWITH_EIGEN=ON -DENABLE_CXX11=ON -DOPENCV_SKIP_PYTHON_LOADER=ON
 -DOPENCV_PYTHON3_INSTALL_PATH=/${py3_sitelib}
 -DOPENCV_PYTHON_INSTALL_PATH=/${py2_sitelib}
 -DOPENCV_EXTRA_MODULES_PATH=${XBPS_BUILDDIR}/opencv_contrib-${version}/modules
 -DBUILD_opencv_rgbd=OFF"
hostmakedepends="pkg-config eigen python-numpy python3-numpy"
makedepends="ffmpeg-devel libpng-devel libjpeg-turbo-devel tiff-devel
 jasper-devel ocl-icd-devel libgomp-devel libopenexr-devel gtk+3-devel
 libgphoto2-devel gst-plugins-base1-devel openblas-devel
 python-numpy python3-numpy python-devel python3-devel"
short_desc="Computer vision and machine learning software library, version 4.x"
maintainer="Hazel Levine <me@qtp2t.club>"
license="BSD-3-Clause"
homepage="https://opencv.org"
distfiles="https://github.com/opencv/opencv/archive/${version}.tar.gz
	https://github.com/opencv/opencv_contrib/archive/${version}.tar.gz>contrib.tar.gz"
checksum="68bc40cbf47fdb8ee73dfaf0d9c6494cd095cf6294d99de445ab64cf853d278a
	acb8e89c9e7d1174e63e40532125b60d248b00e517255a98a419d415228c6a55"

if [ "$XBPS_TARGET_NO_ATOMIC8" ]; then
	makedepends+=" libatomic-devel"
fi

if [ -z "$XBPS_CHECK_PKGS" ]; then
	# opencv by default builds tests that only seem to be used in make check
	configure_args+=" -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF"
fi

if [ "$CROSS_BUILD" ]; then
	# Tell opencv where to find python and numpy
	_npincdir="numpy/core/include"
	configure_args+=" -DPYTHON2_INCLUDE_PATH=${XBPS_CROSS_BASE}/${py2_inc}
	 -DPYTHON2_NUMPY_INCLUDE_DIRS=${XBPS_CROSS_BASE}/${py2_sitelib}/${_npincdir}
	 -DPYTHON3_INCLUDE_PATH=${XBPS_CROSS_BASE}/${py3_inc}
	 -DPYTHON3_NUMPY_INCLUDE_DIRS=${XBPS_CROSS_BASE}/${py3_sitelib}/${_npincdir}"
fi

# Liberation
# Some of the code is taken from:
# https://git.parabola.nu/abslibre.git/tree/libre/opencv/PKGBUILD
post_patch() {
	# remove nonfree Milky icons
	rm -rv modules/highgui/src/files_Qt/Milky

	# remove nonfree lena.png and lena.jpg images
	rm -v $(find . -iname '*lena*')

	cd "${XBPS_BUILDDIR}/opencv_contrib-${version}"

	# remove nonfree SIFT and SURF algorithms that are patented in some countries and have some other limitations on the use
	rm -rv modules/xfeatures2d
}

post_install() {
	vlicense LICENSE
}

libopencv4_package() {
	short_desc+=" - library files"
	pkg_install() {
		vmove usr/lib/*.so.*
	}
}

libopencv4-devel_package() {
	short_desc+=" - development files"
	depends="libopencv4>=${version}_${revision}"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/cmake
		vmove usr/lib/pkgconfig
		vmove usr/lib/*.so
		vmove usr/share/opencv4
	}
}

libopencv4-python_package() {
	short_desc+=" - Python2 bindings"
	depends="python-numpy"
	pkg_install() {
		vmove ${py2_sitelib}
	}
}

libopencv4-python3_package() {
	short_desc+=" - Python3 bindings"
	depends="python3-numpy"
	pkg_install() {
		vmove ${py3_sitelib}
	}
}
